package servlet.good;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Goods;
import beans.Info;
import service.GoodService;

@WebServlet("/GoodServlet")
public class GoodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GoodServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GoodService service = new GoodService();
		
		String page = request.getParameter("page"); // 第几页
		if (page == null)
			page = "1";
		String id = request.getParameter("id"); // 商铺号
		String name = request.getParameter("name"); // 商品名称
		Info<Goods> info = null;
		if (name != null && !name.equals("")) {
			request.setAttribute("name", name);
			info = service.getGoodsByName(name, page, id);
		}else {
			info = service.getGoods(page, id);
		}
		
		request.setAttribute("id", id);
		request.setAttribute("goods", info.list);
		request.setAttribute("page", page);
		request.setAttribute("count", info.count);
		request.getRequestDispatcher("/admin/goods.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
